/**
 * axios的二次封装
*/

import axios from 'axios';

//引入仓库
import {useUserStore} from "@/store/user.js";

//1. 创建axios对象
const service = axios.create();

//2. 请求拦截器
service.interceptors.request.use(config => {
  let userStore = useUserStore();  //注意这里得写在函数里面，vue组件中可随意，避免vue没有挂载pinia的情况
  let token = userStore.token;
  if(token){
    config.headers['Authorization'] = token;
  }

  return config;
}, error => {
  Promise.reject(error);
});

//3. 响应拦截器
service.interceptors.response.use(response => {
  //判断code码
  return response.data;
},error => {
  return Promise.reject(error);
});

export default service;